
//
//  HYLocationManager.m
//  HYNeiHan
//
//  Created by 上官惠阳 on 16/9/25.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "HYLocationManager.h"
#import <AMapLocationKit/AMapLocationKit.h>

@interface HYLocationManager () <AMapLocationManagerDelegate>
@property (nonatomic, strong) AMapLocationManager *locationManager;
@end

static HYLocationManager *_singleton = nil;
@implementation HYLocationManager {
    HYLocationManagerDidUpdateLocationHandle _updateLocationHandle;
}

+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _singleton = [[HYLocationManager alloc] init];
    });
    return _singleton;
}

- (void)setUpLocationManagerUpdateLocationHandle:(HYLocationManagerDidUpdateLocationHandle)updateLocationHandle {
    _updateLocationHandle = updateLocationHandle;
}

// 开始定位
- (void)startSerialLocation {

    if (self.canLocationFlag == NO) {
        return ;
    }
    [self.locationManager startUpdatingLocation];
}

// 停止定位
- (void)stopSerialLocation {
    [self.locationManager stopUpdatingLocation];
}

#pragma mark - AMapLocationManager
// 定位错误
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error = %@",  error);
}

// 定位结果
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)newLocation {


    // 纬度
    NSString *currentLatitude = [[NSString alloc]
                                 initWithFormat:@"%f",
                                 newLocation.coordinate.latitude];

    // 经度
    NSString *currentLongitude = [[NSString alloc]
                                  initWithFormat:@"%f",
                                  newLocation.coordinate.longitude];

    [[NSUserDefaults standardUserDefaults] setObject:currentLatitude forKey:HYUserCurrentLatitude];
    [[NSUserDefaults standardUserDefaults] setObject:currentLongitude forKey:HYUserCurrentLongitude];
    [[NSUserDefaults standardUserDefaults] synchronize];

    if (_updateLocationHandle) {
        _updateLocationHandle(newLocation, currentLatitude, currentLongitude);
    }

}

- (AMapLocationManager *)locationManager {
    if (!_locationManager) {
        AMapLocationManager *locationManager = [[AMapLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.pausesLocationUpdatesAutomatically = NO;
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        locationManager.allowsBackgroundLocationUpdates = NO;
    }
    return _locationManager;
}

- (BOOL)isCanLocationFlag {
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)) {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)hasLocation {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:HYUserCurrentLongitude] length] && [[[NSUserDefaults standardUserDefaults] objectForKey:HYUserCurrentLatitude] length];
}
@end
